package com.quan.site.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.quan.commons.core.MyBaseServiceImpl;
import com.quan.commons.utils.PageUtils;
import com.quan.site.commons.vo.SitePageCateVo;
import com.quan.site.entity.SiteNav;
import com.quan.site.entity.SitePageCate;
import com.quan.site.mapper.SitePageCateMapper;
import com.quan.site.service.SiteNavService;
import com.quan.site.service.SitePageCateService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@Service
public class SitePageCateServiceImpl extends MyBaseServiceImpl<SitePageCateMapper, SitePageCate> implements SitePageCateService {

    @Autowired
    private SitePageCateMapper mapper;

    @Autowired
    private SiteNavService siteNavService;

    @Override
    public SitePageCateVo getInfo(Long id) {
        final SitePageCate projectCenter = super.getById(id);

        SitePageCateVo bean = new SitePageCateVo();
        BeanUtils.copyProperties(projectCenter, bean);

        if (null != bean.getNavId()) {
            // 部门反推获取节点的父路径
            bean.setCascaderNavPathVals(this.siteNavService.findPath(bean.getNavId()));
        }

        return bean;
    }

    @Override
    public List<SitePageCateVo> queryList(PageUtils pageUtils, SitePageCateVo vo) {
        QueryWrapper<SitePageCate> queryWrapper = new QueryWrapper<SitePageCate>(vo);

        List<SitePageCate> list = super.list(pageUtils, queryWrapper);
        List<SitePageCateVo> collect = list.stream().map(item -> {
            SitePageCateVo bean = new SitePageCateVo();
            BeanUtils.copyProperties(item, bean);
            return bean;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public PageUtils queryPage(PageUtils pageUtils, SitePageCateVo vo) {
        QueryWrapper<SitePageCate> queryWrapper = new QueryWrapper<SitePageCate>(vo);

        if (StringUtils.isNotBlank(vo.getKeyword())) {
            queryWrapper.and(w -> {
                w.like("name", vo.getKeyword());
            });
        }

        final IPage<SitePageCate> page = super.selectPage(pageUtils, queryWrapper);

        PageUtils ps = new PageUtils(page);

        final List<SitePageCate> records = page.getRecords();

        final List<SitePageCate> collect = records.stream().map((item) -> {
            SitePageCateVo bean = new SitePageCateVo();
            BeanUtils.copyProperties(item, bean);

            // 获取导航名称
            if (null != item.getNavId()) {
                final SiteNav siteNav = this.siteNavService.getById(item.getNavId());
                if (null != siteNav) {
                    bean.setNavName(siteNav.getName());
                }
            }


            return bean;
        }).collect(Collectors.toList());

        ps.setRecords(collect);
        return ps;
    }
}